'''
Created on Apr 12, 2012

@author: ke
'''

from google.appengine.ext import webapp
#from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
#from google.appengine.ext.db import djangoforms
from google.appengine.api import users
import urllib2
import numpy as np
import csv
from history import *
import datetime
import tools.AlchemyAPI as nlp
from bs4 import BeautifulSoup
from twitter import Twitter
import tools.twokenize as twokenize
from tools.emoticons import analyze_tweet
from tools.sentiment import *


class TweetSentiment(webapp.RequestHandler):
    
    happy_log_probs, sad_log_probs = readSentimentList('lexicon/twitter_sentiment_list.csv')
    
    def get_tweets(self, query='#yahoo'):
        twitter_search = Twitter(domain='search.twitter.com')
        results = []
        for i in range(1,100):
            try:
                result = twitter_search.search(q=query, rpp=100, page=i, lang='en')
            except:
                break
            result = result['results']
            if len(result)==0:
                break
            for r in result:
                try:
                    results.append((str(r['to_user'] or 0), 
                           int(r['to_user_id'] or 0), 
                           str(r['from_user'] or 0), 
                           int(r['from_user_id'] or 0),
                           str(r['geo'] or 0).encode('ascii','ignore'),
                           str(r['text']),
                           str(r['created_at'][:16] or 0),
                           analyze_tweet(str(r['text'])),
                           self.get_sentiment_score(str(r['text']))[0],
                           self.get_sentiment_score(str(r['text']))[1])
                                   )
                except:
                    continue
        
        dt=[('to_user', np.str_, 50),
            ('to_user_id', np.int32),
            ('from_user', np.str_, 50),
            ('from_user_id', np.int32),
            ('geo', np.str_, 50),
            ('text', np.str_, 300),
            ('created_at', np.datetime_),
            ('emotion', np.str_, 50),
            ('happy', np.float),
            ('sad', np.float)
            ]
        
        
        tweets = np.array(results, dtype=dt)
        #tweets = results
        
        return tweets
    
    def get_sentiment_score(self, content = 'this is a test: good'):
        t = twokenize.tokenize(content)
        th, ts = classifySentiment(t, self.happy_log_probs, self.sad_log_probs)
        return (th, ts)
    
    def get_sentiment(self, tweets):
        if len(tweets)== 0:
            return 0
        score = {}
        for d in set(tweets['created_at']):
            score.setdefault(d)
            subset = tweets[tweets['created_at']==d]
            #subset =subset[subset['happy']<0.5]
            if len(subset)==0:
                score[d]=0
            else:
                hs = float(np.sum(subset['happy']))
                ss = float(np.sum(subset['sad']))
                score[d] = (hs - ss)/float(len(subset))
            
        return score
    
    def get_sentiment_comparison(self, query1, query2, score1, score2):
        date=score1.keys()
        for d in score2.keys():
            if d not in date:
                date.append(d)
        date.sort()
        
        results = []
        for d in date:
            score1.setdefault(d,0)
            score2.setdefault(d,0)
            r={'date': d, query1: score1[d], query2: score2[d]}
            results.append([str(d)[:10],r])
        
        return results
    
    def get(self):
        
        #result = self.get_sentiment_score(content='Thanks to all at #Yahoo helping us get to the top of the world http://pic.twitter.com/BVUpJVLZ')
        
        #tweet1 = self.get_tweets(query='#google')
        #tweet2 = self.get_tweets(query='#yahoo')
        
        #result = analyze_tweet('this is a test :)')
        #result = self.get_sentiment_score()
        html =template.render('templates/twitter_form.html', {})
        self.response.out.write(html)
        
        
    
    def post(self):
        q1 = self.request.get('query1').replace(' ','').encode('ascii', 'ignore')
        t1 = self.get_tweets(query=q1)
        s1 = self.get_sentiment(tweets=t1)
        #result = tweets1
        q2 = self.request.get('query2').replace(' ','').encode('ascii', 'ignore')
        t2 = self.get_tweets(query=q2)
        s2 = self.get_sentiment(tweets=t2)
        
        print self.get_sentiment_score(content='this is bad, worst, terrible')
        
        result = self.get_sentiment_comparison(query1=q1, query2=q2, score1=s1, score2=s2)
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(result)
        
        new_twitter_history = TwitterSentiment()
        new_twitter_history.query1 = q1
        new_twitter_history.query2 = q2
        new_twitter_history.user = users.get_current_user()
        new_twitter_history.query_date = datetime.date.today()
        
        new_twitter_history.put()
